package com.cdfahe.gms.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cdfahe.common.util.StringUtils;
import com.cdfahe.common.util.page.ResultFilter;
import com.cdfahe.gms.mybatis.entity.Drug;
import com.cdfahe.gms.service.IDrugService;
import com.cdfahe.gms.service.IDrugTypeService;

@Controller("drugController")
public class DrugController {
	@Autowired
	private IDrugService drugService;
	@Autowired
	private IDrugTypeService drugTypeService;
	
	@RequestMapping(value = "/alldrugs")
    public String listdrugs(@ModelAttribute ResultFilter<Drug> rf,HttpServletRequest request,Model model) {
		int drugtype = 0;
		String type = request.getParameter("type");
		if (!StringUtils.isEmpty(type)) {
			drugtype = Integer.parseInt(type);
		}
		drugService.listDrugs(rf,drugtype,10);
		model.addAttribute("rf", rf);
		model.addAttribute("typeid", drugtype);
		return "drug/druglist";
    }
	
	@RequestMapping(value = "/showdrug")
    public String drugsDetail(HttpServletRequest request,Model model) {
		String drugid =  request.getParameter("drugid");
		Drug drug = drugService.getDrugById(Integer.parseInt(drugid));
		String drugTypeName = drugTypeService.getDrugTypeNameById(drug.getDrugtype());
		drug.setDrugtypename(drugTypeName);
		model.addAttribute("drug", drug);
		return "drug/drugdetail";
    }
	
	
	
}
